<script>
import { districtUnifiedLogin } from '@/api/login.js';
import { mapMutations } from 'vuex';
import { setToken } from '@/utils/auth';
import { saveLoginSource } from "@/utils/handleLoginSource"
export default {
  name: 'DistrictPage',
  render: function (h) {
    return h() // avoid warning message
  },
  data() {
    return {
      loginUrl: 'http://10.55.52.133:8080/slyy/sso/login', // 133统一登录地址
      logoutUrl: 'http://10.55.52.133:8080/slyy/sso/login/loginOut', // 133统一登出地址
      loaclUrl: window.location.origin + window.location.pathname, // 本地地址
    }
  },
  created() {
    this.handleJumpPage()
  },
  methods: {
    ...mapMutations(['SET_TOKEN']),
    // 处理页面跳转
    async handleJumpPage() {
      let { query } = this.$route;
      // 带有JSESSIONID和token，请求接口获取本系统token
      if (query.JSESSIONID && query.token) {
        let params = {
          jsessionid: `JSESSIONID=${query.JSESSIONID}`,
          tokenR: `token=${query.token}`,
          httpUrl: this.loaclUrl
        }
        let { status, data, errors } = await districtUnifiedLogin(params)
        if (status == 200) { // 获取到本系统token，跳转到首页
          setToken(data[0].token);
          this.SET_TOKEN(data[0].token);
          // 保存登录来源
          saveLoginSource('login133');
          setTimeout(() => {
            // window.open(window.location.origin, '_top')
            this.$router.push({ path: '/index' })
          }, 10)
        } else { // 获取本系统token失败，跳转到本系统登录页面
          this.$confirm(`登录失败,原因：${errors[0].message}`, '提示', {
            confirmButtonText: '知道了',
            showClose: false,
            showCancelButton: false,
            closeOnClickModal: false,
            type: 'error'
          }).then(() => {
            // 执行登出
            window.open(`${this.logoutUrl}?service=${this.loaclUrl}`, '_top');
          })
        }
      } else {
        // 没有JSESSIONID和token 跳转到统一登录
        this.jumpUnifiedLoginAuth()
      }
    },
    // 跳转到统一登录
    jumpUnifiedLoginAuth() {
      // console.log(window.location)
      window.open(`${this.loginUrl}?service=${this.loaclUrl}`, '_top')
    }
  },
}
</script>